package com.stress.testing.common;

import java.util.Properties;
import java.util.Set;

public class TraceContext {
    public static final String STRESS_TESTING_ENABLE_KEY = "stress_testing_enable";
    public static final String TRACE_ID_KEY = "id";

    private Properties props;
    private final String tracePropertyPrefix;
    private final String IDKey;
    private final String stressTestingEnableKey;

    public TraceContext(String tracePropertyPrefix) {
        props = new Properties();
        this.tracePropertyPrefix = tracePropertyPrefix;
        this.IDKey = this.tracePropertyPrefix + TRACE_ID_KEY;
        this.stressTestingEnableKey = this.tracePropertyPrefix + STRESS_TESTING_ENABLE_KEY;
    }

    public void setProperty(String key, String value) {
        this.props.setProperty(key, value);
    }

    public String getProperty(String key) {
        return this.props.getProperty(key);
    }

    public boolean hasProperty(String key) {
        return this.props.keySet().contains(key);
    }

    public Set<String> propertyNames() {
        return props.stringPropertyNames();
    }

    public boolean stressTestingEnable() {
        return "true".equals(getProperty(stressTestingEnableKey));
    }

    public String getTraceIDKey() {
        return IDKey;
    }

    public boolean hasTraceID() {
        String traceID = getTraceID();
        return traceID != null && traceID.length() > 0;
    }

    public String getTraceID() {
        return getProperty(IDKey);
    }

    public void setTraceID(String traceID) {
        if (traceID == null || traceID.length() == 0) {
            throw new IllegalArgumentException("traceID cannot be null or empty");
        }

        setProperty(IDKey, traceID);
    }
}
